﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using StarsAdmin.Core.App;
using StarsAdmin.Core.DB.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace StarsAdmin.Core.DB.InitData
{
    public class InitData
    {
        public static void InitializeData()
        {
            using var scope = AppHelper.ServiceProvider.CreateScope();

            var context = AppHelper.GetService<DBContext>();

            if (context.Menus == null || context.Roles == null || context.Users == null)
            {
                // 确保数据库存在，如果数据库不存在则创建数据库，存在则什么也不做
                context.Database.EnsureCreated();
            }

            // 如果已经有数据，则跳过
            if (context.Menus.Any() || context.Roles.Any() || context.Users.Any())
            {
                return;
            }

            Console.WriteLine(" ----------------------------- 数据库初始化开始 ----------------------------- ");

            var result = ExecuteInitData(context);

            if (result)
            {
                Console.WriteLine(" ----------------------------- 数据库初始化成功 ----------------------------- ");
            } else
            {
                Console.WriteLine(" ----------------------------- 数据库初始化失败 ----------------------------- ");
            }
        }

        private static bool ExecuteInitData(DBContext context)
        {
            var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            // 读取 JSON 文件内容
            var menujson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/menu.json");
            var rolejson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/role.json");
            var userjson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/user.json");
            var roleMenujson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/role_menu.json");
            var userRolejson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/user_role.json");
            var regionjson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/region.json");
            var filejson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/file.json");
            var dictTypejson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/dictType.json");
            var dictjson = System.IO.File.ReadAllText($"{assemblyDirectory}/InitData/dict.json");

            // 解析 JSON 内容为 实体对象
            var menus = JsonConvert.DeserializeObject<List<MenuEntity>>(menujson);
            if (menus.Any())
            {
                context.Menus.AddRange(menus);
            }
            var roles = JsonConvert.DeserializeObject<List<RoleEntity>>(rolejson);
            if (roles.Any())
            {
                context.Roles.AddRange(roles);
            }
            var users = JsonConvert.DeserializeObject<List<UserEntity>>(userjson);
            if (users.Any())
            {
                context.Users.AddRange(users);
            }
            var roleMenus = JsonConvert.DeserializeObject<List<RoleMenuEntity>>(roleMenujson);
            if (roleMenus.Any())
            {
                context.RoleMenus.AddRange(roleMenus);
            }
            var userRoles = JsonConvert.DeserializeObject<List<UserRoleEntity>>(userRolejson);
            if (userRoles.Any())
            {
                context.UserRoles.AddRange(userRoles);
            }
            var regions = JsonConvert.DeserializeObject<List<RegionEntity>>(regionjson);
            if (regions.Any())
            {
                context.Regions.AddRange(regions);
            }
            var files = JsonConvert.DeserializeObject<List<FileEntity>>(filejson);
            if (files.Any())
            {
                context.Files.AddRange(files);
            }
            var dictTypes = JsonConvert.DeserializeObject<List<DictTypeEntity>>(dictTypejson);
            if (dictTypes.Any())
            {
                context.DictTypes.AddRange(dictTypes);
            }
            var dicts = JsonConvert.DeserializeObject<List<DictEntity>>(dictjson);
            if (dicts.Any())
            {
                context.Dicts.AddRange(dicts);
            }

            return context.SaveChanges() > 0;
        }
    }
}